<html>
<script>
    let xhr = (url, rType) => {
        return new Promise((res, rej) => {
            let xhreq = new XMLHttpRequest();
            let success = false;
            xhreq.responseType = rType;
            xhreq.open("GET", url);
            xhreq.send();
            //event reg
            xhreq.onreadystatechange = () => {
                if (xhreq.readyState == 4) {
                    switch (xhreq.status) {
                        case 200:
                            success = true;
                            res(xhreq.response);
                            break;
                        case 404: rej(new Error(`got a 404: ${url}`)); break;
                        default:
                    }
                }
            };
            xhreq.onloadend = () => {
                if (success) return;
                if (!this.enableRetry) {
                    rej(new Error(`load fial: ${url}`));
                    return;
                }
                //加载失败 重试
                console.log("加载失败 重试");
            };
            xhreq.onerror = (ev) => {
                rej(new Error(`load error : ${url}`));
            };
        });
    }

    let formatTex = (tex) => {
        let arr = tex.split("\n");
        let result = [];
        arr.forEach((v) => {
            if (v) {
                result.push(v.split("\r")[0]);
            }
        });
        return result;
    }

    let dataPs = [xhr(`A.txt`, "text"), xhr(`B.txt`, "text")];
    let dataA;
    let dataB;
    Promise.all(dataPs).then((strArr) => {
        dataA = formatTex(strArr[0]);
        dataB = formatTex(strArr[1]);

        let _mapA = {};
        dataA.forEach((v) => {
            _mapA[v] = true;
        });

        let _mapB = {};
        dataB.forEach((v) => {
            _mapB[v] = true;
        });


        let result = "";
        //筛选 A 和 B 共有的
        console.log(`A 和 B 共有的`);
        dataA.forEach((v, i) => {
            if (_mapB[v] && _mapA[v]) {
                result += `${v}\n`;
            }
        });
        console.log(result);

        //筛选 A 在 B 中没有
        result = "";
        console.log(`A 在 B 中没有的`);
        dataA.forEach((v, i) => {
            if (!_mapB[v]) result += `${v}\n`;
        });
        console.log(result);



        //筛选 A 在 B 中没有
        result = "";
        console.log(`B 在 A 中没有的`);
        dataB.forEach((v, i) => {
            if (!_mapA[v]) result += `${v}\n`;
        });
        console.log(result);

    });

</script>

</html>